<?php
/**
 * File: xmlToArray.php
 * 
 * This file will accept a cURL object which will contain XML formatted data.
 * 
 * This class will accept the cURL object and parse the object using simpleXML
 * parser which will store all the parsed xml data into a multi-dimensional array.
 * 
 * @version 1.0
 *
 */
/**
 * 
 */
/**
 * xmlToArray class
 * 
 * This class parses the cURL object and returns a mult-dimensional array
 * containing the data from the XML.
 * 
 * @return arrOutput The multi-dimensional array that contains the XML data
 *
 */
class xmlToArray {
	 
	var $arrOutput = array();
	var $resParser;
	var $strXmlData;
	 
	/**
	 * Function to parse the cURL object being passed in.
	 *
	 * @param cURL object $strInputXML
	 * @return $arrOutput The multi-dimensional array that contains the XML data
	 */
	function parseXML($strInputXML) {
		 
		$this->resParser = xml_parser_create ();
		xml_set_object($this->resParser,$this);
		xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
		 
		xml_set_character_data_handler($this->resParser, "tagData");
		 
		$this->strXmlData = xml_parse($this->resParser,$strInputXML );
		if(!$this->strXmlData) {
			die(sprintf("XML error: %s at line %d",
			xml_error_string(xml_get_error_code($this->resParser)),
			xml_get_current_line_number($this->resParser)));
		}
		 
		xml_parser_free($this->resParser);
		 
		return $this->arrOutput;
	}
	
	/**
	 * Function to handle the open XML tag.
	 *
	 * @param simpleXML object $parser The parser object
	 * @param $name The name of the open tag
	 * @param $attrs The attributes if the open tag has any
	 */
	function tagOpen($parser, $name, $attrs) {
		$tag=array("Name"=>$name,"attributes"=>$attrs);
		array_push($this->arrOutput,$tag);
	}
	 
	/**
	 * Function to handle the data between the XML tags.
	 *
	 * @param $parser The parser object
	 * @param $tagData The data inside the tags
	 */
	function tagData($parser, $tagData) {
		if(trim($tagData)) {
			if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
				$this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
			}
			else {
				$this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
			}
		}
	}
	 
	/**
	 * Function to handle the closing tag
	 *
	 * @param $parser The parser object
	 * @param $name The name of the closing tag
	 */
	function tagClosed($parser, $name) {
		$this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
		array_pop($this->arrOutput);
	}
}
?>